import Vue from 'vue'
import VueRouter from 'vue-router'
import { login, home } from '@/router/routers.js'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)

const routes = [
  {
    name: 'index',
    path: '/',
    redirect: '/login'
  },
  login,
  home
]

const router = new VueRouter({
  routes
})
// 路由导航守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  // console.log(to.path)
  if (to.path !== '/login' && !Vue.prototype.$storage.get('token')) {
    Vue.prototype.$message.error('登录态失效请重新登录！')
    return next('/login')
  }
  // if (to.meta.title) {
  //   // 设置title
  //   window.document.title = to.meta.title
  // }
  window.document.title = '后台系统'
  next()
})
router.afterEach(() => {
  NProgress.done()
})
export default router
